package us.ajg0702.leaderboards.heads;

import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.block.Skull;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import us.ajg0702.leaderboards.Main;
import us.ajg0702.leaderboards.cache.Cache;
import us.ajg0702.leaderboards.libs.spigot.VersionSupport;
import us.ajg0702.leaderboards.signs.BoardSign;

import java.util.Collection;
import java.util.Objects;
import java.util.UUID;

public class HeadManager {
    static HeadManager instance;
    Main pl;

    private HeadManager(Main pl) {
        this.pl = pl;
    }

    public static HeadManager getInstance() {
        return instance;
    }

    public static HeadManager getInstance(Main pl) {
        if (instance == null) {
            instance = new HeadManager(pl);
        }
        return instance;
    }

    public void search(BoardSign sign, String name, UUID id) {
        BlockFace face;
        int z, x;
        if (!sign.getLocation().getBlock().getType().toString().contains("SIGN"))
            return;
        if (!Cache.getInstance().boardExists(sign.getBoard()))
            return;
        if (id == null)
            return;
        Sign ss = sign.getSign();

        if (VersionSupport.getMinorVersion() > 12) {
            BlockData bd = ss.getBlockData();
            if (bd instanceof Sign) {
                Sign bs = (Sign) bd;
                face = bs.getRotation();
            } else {
                WallSign bs = (WallSign) bd;
                face = bs.getFacing();
            }
        } else {
            Sign bs = (Sign) ss.getData();
            face = bs.getFacing();
        }

        Location sl = sign.getLocation();


        switch (face) {


            case NORTH:
            case NORTH_EAST:
            case NORTH_NORTH_EAST:
            case NORTH_NORTH_WEST:
            case NORTH_WEST:
                for (z = sl.getBlockZ() + 1; z > sl.getBlockZ() - 1; z--) {
                    for (int y = sl.getBlockY() + 1; y > sl.getBlockY() - 1; y--) {
                        Location curloc = new Location(sl.getWorld(), sl.getX(), y, z);
                        debugParticles(curloc);

                        checkHead(curloc, name, id);
                        checkArmorstand(curloc, name, id);
                    }
                }
                break;


            case SOUTH:
            case SOUTH_EAST:
            case SOUTH_SOUTH_EAST:
            case SOUTH_SOUTH_WEST:
            case SOUTH_WEST:
                for (z = sl.getBlockZ(); z > sl.getBlockZ() - 2; z--) {
                    for (int y = sl.getBlockY() + 1; y > sl.getBlockY() - 1; y--) {
                        Location curloc = new Location(sl.getWorld(), sl.getX(), y, z);
                        debugParticles(curloc);

                        checkHead(curloc, name, id);
                        checkArmorstand(curloc, name, id);
                    }
                }
                break;


            case EAST:
            case EAST_NORTH_EAST:
            case EAST_SOUTH_EAST:
                for (x = sl.getBlockX(); x > sl.getBlockX() - 2; x--) {
                    for (int y = sl.getBlockY() + 1; y > sl.getBlockY() - 1; y--) {
                        Location curloc = new Location(sl.getWorld(), x, y, sl.getZ());
                        debugParticles(curloc);

                        checkHead(curloc, name, id);
                        checkArmorstand(curloc, name, id);
                    }
                }
                break;


            case WEST:
            case WEST_NORTH_WEST:
            case WEST_SOUTH_WEST:
                for (x = sl.getBlockX() + 1; x > sl.getBlockX() - 1; x--) {
                    for (int y = sl.getBlockY() + 1; y > sl.getBlockY() - 1; y--) {
                        Location curloc = new Location(sl.getWorld(), x, y, sl.getZ());
                        debugParticles(curloc);

                        checkHead(curloc, name, id);
                        checkArmorstand(curloc, name, id);
                    }
                }
                break;
        }
    }


    private void setArmorstandHead(ArmorStand stand, String name, UUID id) {
        if (VersionSupport.getMinorVersion() >= 10) {
            stand.setSilent(true);
        }
        ItemStack item = HeadUtils.getInstance().getHeadItem(name);
        stand.setHelmet(item);
    }


    private void debugParticles(Location curloc) {
    }


    private void checkHead(final Location loc, final String name, final UUID id) {
        Validate.notNull(loc);
        Validate.notNull(id, "UUID is null!");

        Bukkit.getScheduler().runTaskAsynchronously(this.pl, () -> {
            final OfflinePlayer op = Bukkit.getOfflinePlayer(id);
            Bukkit.getScheduler().runTask(this.pl, new Runnable() {
                public void run() {
                    BlockState bs = loc.getBlock().getState();
                    if (!(bs instanceof Skull))
                        return;
                    if (HeadUtils.getInstance().getHeadValue(name).equals(""))
                        return;
                    Skull skull = (Skull) bs;
                    boolean update = false;
                    if (VersionSupport.getMinorVersion() > 9) {
                        if (skull.hasOwner()) {
                            if (Objects.equals(skull.getOwningPlayer().getUniqueId(), id)) {
                                skull.setOwningPlayer(op);
                                update = true;
                            }
                        } else {
                            skull.setOwningPlayer(op);
                            update = true;
                        }

                    } else if (!Objects.equals(skull.getOwner(), name)) {
                        skull.setOwner(name);
                        update = true;
                    }

                    if (update) skull.update();

                }
            });
        });
    }

    private void checkArmorstand(final Location curloc, final String name, final UUID id) {
        final Collection<Entity> entities = curloc.getWorld().getNearbyEntities(curloc, 1.0D, 1.0D, 1.0D);
        if (entities.size() > 0)
            Bukkit.getScheduler().runTaskAsynchronously(this.pl, new Runnable() {
                public void run() {
                    for (Entity entity : entities) {
                        if (entity instanceof ArmorStand) {
                            Location eloc = entity.getLocation();
                            if (eloc.getBlockX() != curloc.getBlockX() || eloc.getBlockZ() != curloc.getBlockZ())
                                continue;
                            HeadManager.this.setArmorstandHead((ArmorStand) entity, name, id);
                        }
                    }
                }
            });
    }
}


*Location:D:\User\Xuan\Downloads\ajLeaderboards-1.3.3.jar\\us\ajg0702\leaderboards\heads\HeadManager.class
 *Java compiler version:8(52.0)
         *JD-Core Version:1.1.3
